package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.RegularUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.mapper.CustomerMapper;
import com.dqyt.khjc.mapper.CustomerUserMapper;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户信息Service
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private CustomerUserMapper customerUserMapper;

    /**
     * 查询所有未删除的客户信息
     * 在待审核申请页面的下拉框 调用
     *
     * @return
     */
    public ResultMsg getCustomerList(HttpServletRequest request) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以管理待审核申请
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_DSHSQ)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getCustomerList();
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询所有未删除的客户信息
     * 在新增客户人员信息下拉框 调用
     *
     * @return
     */
    public ResultMsg getXzkhrylCustomerList(HttpServletRequest request) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getCustomerList();
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 查询所有未删除的客户信息  一級單位
     * 在新增客户人员信息下拉框 调用
     *
     * @return
     */
    public ResultMsg getXzkhrylCustomerListPar(HttpServletRequest request) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getCustomerListPar();
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 查询所有未删除的客户信息  一級單位
     * 在新增客户人员信息下拉框 调用
     *
     * @return
     */
    public ResultMsg getXzkhrylCustomerListCha(HttpServletRequest request,int pId) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getXzkhrylCustomerListCha(pId);
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询所有未删除的客户信息
     * 在客户人员信息一览 调用
     *
     * @return
     */
    public ResultMsg getKhxxylCustomerList(HttpServletRequest request) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以管理新增客户人员信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_KHRYXXYL)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getCustomerList();
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 查询所有未删除的客户信息
     * 样品管理 样品登记 调用
     *
     * @return
     */
    public ResultMsg getypglCustomerList(HttpServletRequest request) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
//            // 用户权限检查 是否可以管理新增客户人员信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_SJYPDJ)
//                    || !CheckTool.checkAuthority(user, AuthorityID.YPGL_YPYL)
//                    || !CheckTool.checkAuthority(user, AuthorityID.SBWX_WXLB)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getCustomerList();
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }












    /**
     * 新增客户信息
     *
     * @param enterpriseName 客户名称
     * @param province       省
     * @param city           市
     * @param block          区
     * @param address        地址
     * @param contacts       联系人
     * @param phone          联系人手机
     * @param email          电子邮箱
     * @param telephone      座机
     * @param fax            传真
     * @return
     */
    public ResultMsg addCustomer(HttpServletRequest request, String enterpriseName, int province, int city, int block,
                                 String address, String contacts, String phone, String email, String telephone, String fax) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_XZKHXX)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            //参数判断
            if (null == enterpriseName ||"".equals(enterpriseName) || enterpriseName.length() > 40||
                    province == 0 || city == 0 || block == 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }

            //更具名称查询是否重名
           long mcount =  customerMapper.getcustomerByName(0,enterpriseName);
            if(mcount>0){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("已经存在该企业");
                return ret;
            }

            CustomerBean customerBean = new CustomerBean();
            customerBean.setpId(0);
            customerBean.setEnterpriseName(enterpriseName);
            customerBean.setProvince(province);
            customerBean.setpName("");
            customerBean.setCity(city);
            customerBean.setBlock(block);
            customerBean.setAddress(address);
            customerBean.setContacts(contacts);
            customerBean.setPhone(phone);
            customerBean.setEmail(email);
            customerBean.setTelephone(telephone);
            customerBean.setExamineType(1);
            customerBean.setFax(fax);
            customerBean.setAddTime(TimeUtil.currentTimeMillis());
            customerBean.setAddUser(String.valueOf(user.getUserId()));
            customerBean.setUpdateTime(TimeUtil.currentTimeMillis());
            customerBean.setUpdateUser(String.valueOf(user.getUserId()));
            int res = customerMapper.addCustomer(customerBean);
            int res2 =  customerMapper.updatePxzd(customerBean.getCustomerId(),customerBean.getCustomerId());
            if (res <= 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
                //
                customerMapper.deleteById(customerBean.getCustomerId());
                return ret;
            }else{
                if(res2>0){
                    ret.setCode(ResultMsg.SUCESS);
                    ret.setMsg("添加成功");
                }else{
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("数据异常");
                    return ret;
                }
            }

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 新增了一条客户名称为： [" + enterpriseName + "] 的客户信息");
                historyBean.setType(Opt.KHGL_XZKHXX_XZ);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(customerBean.getCustomerId()));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 新增客户信息
     *
     * @param enterpriseName 客户名称
     * @param province       省
     * @param city           市
     * @param block          区
     * @param address        地址
     * @param contacts       联系人
     * @param phone          联系人手机
     * @param email          电子邮箱
     * @param telephone      座机
     * @param fax            传真
     * @return
     */
    public ResultMsg addCustomerCh(HttpServletRequest request,int pid,String pName, String enterpriseName, int province, int city, int block,
                                 String address, String contacts, String phone, String email, String telephone, String fax) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_XZKHXX)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            //参数判断
            if (null == enterpriseName ||"".equals(enterpriseName) || enterpriseName.length() > 40||
                    province == 0 || city == 0 || block == 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }



            //更具名称查询是否重名
            long mcount =  customerMapper.getcustomerByName(pid,enterpriseName);
            if(mcount>0){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("已经存在该企业");
                return ret;
            }

            CustomerBean customerBean = new CustomerBean();
            customerBean.setpId(pid);
            customerBean.setEnterpriseName(enterpriseName);
            customerBean.setProvince(province);
            customerBean.setCity(city);
            customerBean.setpName(pName);
            customerBean.setPxzd(pid);
            customerBean.setBlock(block);
            customerBean.setAddress(address);
            customerBean.setContacts(contacts);
            customerBean.setPhone(phone);
            customerBean.setEmail(email);
            customerBean.setTelephone(telephone);
            customerBean.setExamineType(1);
            customerBean.setFax(fax);
            customerBean.setAddTime(TimeUtil.currentTimeMillis());
            customerBean.setAddUser(String.valueOf(user.getUserId()));
            customerBean.setUpdateTime(TimeUtil.currentTimeMillis());
            customerBean.setUpdateUser(String.valueOf(user.getUserId()));
            int res = customerMapper.addCustomerCh(customerBean);
            if (res <= 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
                return ret;
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("添加成功");
            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 新增了一条客户名称为： [" + enterpriseName + "] 的客户信息");
                historyBean.setType(Opt.KHGL_XZKHXX_XZ);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(customerBean.getCustomerId()));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 新增客户信息
     *
     * @param enterpriseName 客户名称
     * @param province       省
     * @param city           市
     * @param block          区
     * @param address        地址
     * @param contacts       联系人
     * @param phone          联系人手机
     * @param email          电子邮箱
     * @param telephone      座机
     * @param fax            传真
     * @return
     */
    public ResultMsg updateCustomer(HttpServletRequest request,int customerId, String enterpriseName, int province, int city, int block,
                                 String address, String contacts, String phone, String email, String telephone, String fax) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_XZKHXX)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }



            //参数判断
            if (customerId == 0 ||null == enterpriseName ||"".equals(enterpriseName) || enterpriseName.length() > 40||
                    province == 0 || city == 0 || block == 0
                    ) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            CustomerBean customerBean = customerMapper.getCustomerById(customerId);


//            //更具名称查询是否重名
//            long mcount =  customerMapper.getcustomerByName(enterpriseName);
//            if(mcount>0){
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("已经存在该企业");
//                return ret;
//            }


            customerBean.setEnterpriseName(enterpriseName);
            customerBean.setProvince(province);
            customerBean.setCity(city);
            customerBean.setBlock(block);
            customerBean.setAddress(address);
            customerBean.setContacts(contacts);
            customerBean.setPhone(phone);
            customerBean.setEmail(email);
            customerBean.setTelephone(telephone);
            customerBean.setFax(fax);
            customerBean.setUpdateTime(TimeUtil.currentTimeMillis());
            customerBean.setUpdateUser(String.valueOf(user.getUserId()));
            int res = customerMapper.updateCustomer(customerBean);
            if (res <= 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
                return ret;
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("修改成功");
            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "] 修改了客户名称为： [" + enterpriseName + "] 的客户信息");
                historyBean.setType(Opt.KHGL_KHXXYL_XG);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(customerBean.getCustomerId()));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 分页查询 客户信息
     *
     * @param page
     * @param size
     * @param enterpriseName 客户名称
     * @param contacts       联系人
     * @param loginStatue    限制登陆状态
     * @param province       省
     * @param city           市
     * @param block          区
     * @param address        详细地址
     * @return
     */
    public ResultMsg getCustomerListPage(HttpServletRequest request, Integer page, Integer size, String enterpriseName, String contacts,
                                         String loginStatue, Integer province, Integer city, Integer block, String address) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_KHXXYL)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            //分页查询数据
            List<CustomerBean> customerBeans = customerMapper.getCustomerListPage(page, size, enterpriseName, contacts, loginStatue, province, city, block, address);



            long total = customerMapper.getCustomerTotalNumber(enterpriseName, contacts, loginStatue, province, city, block, address);
            RespPageBean bean = new RespPageBean();
            bean.setData(customerBeans);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 允许登陆 / 禁止登陆
     *
     * @param customerId
     * @param loginStatue    1:允许登陆    0禁止登陆
     * @param enterpriseName 客户名称
     * @return
     */
    public ResultMsg loginRestrictions(HttpServletRequest request, int customerId, int loginStatue, String enterpriseName) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.KHGL_KHXXYL)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            //参数检查
            if (customerId == 0  || loginStatue>1) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            int res = customerMapper.loginRestrictions(customerId, loginStatue);
            if (res <= 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
                return ret;
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("操作成功");

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();

                if (loginStatue == 0) {
                    historyBean.setMessage("[" + user.getUserName() + "] 禁止了： [" + enterpriseName + "] 登陆");
                    historyBean.setType(Opt.KHGL_KHXXYL_JZDL);
                } else {
                    historyBean.setMessage("[" + user.getUserName() + "] 允许了： [" + enterpriseName + "] 登陆");
                    historyBean.setType(Opt.KHGL_KHXXYL_YXDL);
                }
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(customerId));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 删除客户信息
     * @param request
     * @param customerId
     * @return
     */
    public ResultMsg deleteCustomer(HttpServletRequest request, int customerId,String enterpriseName) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);


            //CustomerBean customerBean=  customerMapper.getCustomerById(customerId);

           int res = customerMapper.deleteById(customerId);



            if(res>0){
                //删除子公司
                customerMapper.deleteByPId(customerId);
                //并删除客户下面的所有人员
               int dc= customerUserMapper.deleteAllByChId(customerId);
               int dc2= customerUserMapper.deleteAllByCustomerId(customerId);

                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                //如果操作日志的开关已经打开
                if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                    //添加操作记录
                    // 先取得用户ip
                    String ip = IpUtil.getIpAddr(request);
                    HistoryBean historyBean = new HistoryBean();
                    historyBean.setMessage("[" + user.getUserName() + "] 删除了客户名称为： [" + enterpriseName + "] 的信息");
                    historyBean.setType(Opt.KHGL_KHRYXXYL_SC);
                    historyBean.setOpUserId(user.getUserId());
                    historyBean.setIp(ip);
                    historyBean.setBopId(String.valueOf(customerId));
                    historyBean.setAddTime(TimeUtil.currentTimeMillis());
                    historyBean.setAddUser(String.valueOf(user.getUserId()));
                    historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                    historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                    //插入数据库
                    int ress = historyMapper.addHistory(historyBean);
                }

            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候再试！");
                return ret;
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




}
